package com.qf.android.huihui.adapter.information;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qf.android.huihui.R;
import com.qf.android.huihui.bean.DetailListBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lx on 2017/1/9.
 */

public class DetailListAdapter extends RecyclerView.Adapter<DetailListAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private LayoutInflater inflater;
    private DetailListBean bean;
    private DetailListOnClickLister detailListener;
    private RecyclerView rv;

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        this.rv = recyclerView;
        super.onAttachedToRecyclerView(recyclerView);
    }

    public interface DetailListOnClickLister {
        void onItemClickListener(int position, View view);
    }

    public DetailListAdapter(Context context, DetailListBean bean) {
        this.context = context;
        this.bean = bean;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.detail_list_item, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        String url = bean.getData().get(position).getImage_url();
        List<DetailListBean.DataBean.ItemsBean> items = bean.getData().get(position).getItems();
        Picasso.with(context).load(url).error(R.drawable.ic_default_img).into(holder.iv);

        Picasso.with(context).load(items.get(0).getImage_url()).error(R.drawable.ic_default_img).into(holder.iv_1);
        Picasso.with(context).load(items.get(1).getImage_url()).error(R.drawable.ic_default_img).into(holder.iv_2);
        Picasso.with(context).load(items.get(2).getImage_url()).error(R.drawable.ic_default_img).into(holder.iv_3);
        Picasso.with(context).load(items.get(3).getImage_url()).error(R.drawable.ic_default_img).into(holder.iv_4);
        Picasso.with(context).load(items.get(4).getImage_url()).error(R.drawable.ic_default_img).into(holder.iv_5);


    }

    @Override
    public int getItemCount() {
        return bean.getData().size();
    }

    public void onItemClickListener(DetailListOnClickLister detailListener) {
        this.detailListener = detailListener;
    }

    @Override
    public void onClick(View view) {
        if (detailListener != null) {
            detailListener.onItemClickListener(rv.getChildAdapterPosition(view), view);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private ImageView iv_1, iv_2, iv_3, iv_4, iv_5;
        private LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.detail_iv);
            iv_1 = (ImageView) itemView.findViewById(R.id.iv_1);
            iv_2 = (ImageView) itemView.findViewById(R.id.iv_2);
            iv_3 = (ImageView) itemView.findViewById(R.id.iv_3);
            iv_4 = (ImageView) itemView.findViewById(R.id.iv_4);
            iv_5 = (ImageView) itemView.findViewById(R.id.iv_5);


        }
    }
}